<template>
  <div class="m-[20px]">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="auto">
      <el-form-item label="图片路径" prop="folder">
        <el-input v-model="form.folder" readonly placeholder="请选择路径">
          <template #append>
            <el-button :loading="loading" :icon="Files" @click="chooseFolder" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="输出路径" prop="outputFolder">
        <el-input v-model="form.outputFolder" readonly placeholder="请选择路径">
          <template #append>
            <el-button :icon="Files" @click="chooseOutputFolder" />
          </template>
        </el-input>
      </el-form-item>
    </el-form>
    <div v-if="tableData.length" class="flex items-center mb-5">
      <el-button
        class="flex-1"
        type="primary"
        :loading="loading"
        @click="submit"
        >{{ loading ? '正在执行' : '执行修改' }}</el-button
      >
      <el-button class="flex-1" type="success" @click="tableData = []"
        >清空列表</el-button
      >
    </div>
    <div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" />
        <el-table-column
          prop="file_name"
          label="文件名"
          show-overflow-tooltip
        />
        <el-table-column prop="md5" label="原先的MD5" show-overflow-tooltip />
        <el-table-column
          prop="new_md5"
          label="修改后的MD5"
          show-overflow-tooltip
        />
      </el-table>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Files } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import {
  ChooseFolder,
  GetImageFiles,
  CalculateMD5
} from '../../../wailsjs/go/main/App'
import type { main } from '../../../wailsjs/go/models'
import { ElMessage } from 'element-plus'
import type { FormInstance } from 'element-plus'

const formRef = ref<FormInstance>()

const chooseFolder = () => {
  loading.value = true
  GetImageFiles()
    .then(result => {
      if (!result) {
        loading.value = false
        ElMessage.warning('取消操作')
        return
      }
      form.folder = result.folder
      form.outputFolder = result.folder + '/output'
      tableData.value = result.files
      loading.value = false
    })
    .catch(() => {
      loading.value = false
    })
}

const chooseOutputFolder = () => {
  ChooseFolder().then(result => {
    if (!result) return
    form.outputFolder = result
  })
}

const form = reactive({
  folder: '',
  outputFolder: ''
})

const rules = {
  folder: [{ required: true, message: '请选择图片路径', trigger: 'blur' }],
  outputFolder: [{ required: true, message: '请选择输出路径', trigger: 'blur' }]
}

const loading = ref(false)
const tableData = ref<main.FileMD5[]>([])

const submit = () => {
  formRef.value!.validate(valid => {
    if (!valid) return
    loading.value = true
    CalculateMD5({
      output_folder: form.outputFolder,
      files: tableData.value
    } as main.UpdateMd5Request)
      .then(result => {
        if (!result) {
          ElMessage.error('执行失败')
        } else {
          tableData.value = result
          ElMessage.success('执行成功')
        }
        loading.value = false
      })
      .catch(() => {
        loading.value = false
      })
  })
}
</script>
